{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Flow Plot Example\n",
    "\n",
    "Here, we are going to import a network and plot the electricity flow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pypsa, os\n",
    "import cartopy.crs as ccrs\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "\n",
    "import warnings\n",
    "from shapely.errors import ShapelyDeprecationWarning\n",
    "\n",
    "warnings.filterwarnings(\"ignore\", category=ShapelyDeprecationWarning)\n",
    "plt.rc(\"figure\", figsize=(10, 8))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Import and optimize a network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = pypsa.examples.ac_dc_meshed(from_master=True)\n",
    "n.optimize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get mean generator power by bus and carrier:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gen = n.generators.assign(g=n.generators_t.p.mean()).groupby([\"bus\", \"carrier\"]).g.sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plot the electricity flows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# links are not displayed for prettier output ('link_widths=0')\n",
    "n.plot(\n",
    "    bus_sizes=gen / 5e3,\n",
    "    bus_colors={\"gas\": \"indianred\", \"wind\": \"midnightblue\"},\n",
    "    margin=0.5,\n",
    "    flow=\"mean\",\n",
    "    line_widths=0.1,\n",
    "    link_widths=0,\n",
    ")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plot the electricity flows with a different projection and a colored map:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# links are not displayed for prettier output ('link_widths=0')\n",
    "n.plot(\n",
    "    bus_sizes=gen / 5e3,\n",
    "    bus_colors={\"gas\": \"indianred\", \"wind\": \"midnightblue\"},\n",
    "    margin=0.5,\n",
    "    flow=\"mean\",\n",
    "    line_widths=0.1,\n",
    "    link_widths=0,\n",
    "    projection=ccrs.EqualEarth(),\n",
    "    color_geomap=True,\n",
    ")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set arbitrary values as flow argument using the MultiIndex of `n.branches()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flow = pd.Series(10, index=n.branches().index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# links are not displayed for prettier output ('link_widths=0')\n",
    "n.plot(\n",
    "    bus_sizes=gen / 5e3,\n",
    "    bus_colors={\"gas\": \"indianred\", \"wind\": \"midnightblue\"},\n",
    "    margin=0.5,\n",
    "    flow=flow,\n",
    "    line_widths=2.7,\n",
    "    link_widths=0,\n",
    "    projection=ccrs.EqualEarth(),\n",
    "    color_geomap=True,\n",
    ")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Adjust link colors according to their mean load:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pandas series with MultiIndex\n",
    "# links are not displayed for prettier output ('link_widths=0')\n",
    "collection = n.plot(\n",
    "    bus_sizes=gen / 5e3,\n",
    "    bus_colors={\"gas\": \"indianred\", \"wind\": \"midnightblue\"},\n",
    "    margin=0.5,\n",
    "    flow=flow,\n",
    "    line_widths=2.7,\n",
    "    link_widths=0,\n",
    "    projection=ccrs.EqualEarth(),\n",
    "    color_geomap=True,\n",
    "    line_colors=n.lines_t.p0.mean().abs(),\n",
    ")\n",
    "\n",
    "plt.colorbar(collection[2], fraction=0.04, pad=0.004, label=\"Flow in MW\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "",
   "language": "python",
   "name": ""
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d4783b90c31ddc11d3b01e737442d9214182b2321eb6e813ddb1a44db2ad1d92"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
